package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.fragment.CollectFragment;
import cn.dnui_cr503.dnuifood_16110100503.fragment.HomeFragment;
import cn.dnui_cr503.dnuifood_16110100503.fragment.MeFragment;
import cn.dnui_cr503.dnuifood_16110100503.fragment.SearchFragment;


public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private ImageButton home_Button,collect_Button,search_Button,me_Button;
    private Fragment collect,home,me,search;
    private LinearLayout id_shouye,id_collect,id_search,id_me;
    private TextView shouye_text,collect_text,me_text,search_text;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        resetImges();
        setSelect(0);
    }
    private void initView() {
        home_Button=findViewById(R.id.home_Button);
        collect_Button=findViewById(R.id.collect_Button);
        search_Button=findViewById(R.id.search_Button);
        me_Button=findViewById(R.id.me_Button);

        shouye_text=findViewById(R.id.shouye_text);
        collect_text=findViewById(R.id.collect_text);
        me_text=findViewById(R.id.me_text);
        search_text=findViewById(R.id.search_text);

        id_shouye=findViewById(R.id.id_shouye);
        id_collect=findViewById(R.id.id_collect);
        id_search=findViewById(R.id.id_search);
        id_me=findViewById(R.id.id_me);
    }

    private void setSelect(int i) {
        FragmentManager  fm=getSupportFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if(home==null){
                    home= new HomeFragment();
                    transaction.add(R.id.id_content,home);
                }else {
                    transaction.show(home);
                }
                shouye_text.setTextColor(Color.parseColor("#1296db"));
                home_Button.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            case 1:
                if(collect==null){
                    collect= new CollectFragment();
                    transaction.add(R.id.id_content,collect);
                }else {
                    transaction.show(collect);
                }
                collect_text.setTextColor(Color.parseColor("#1296db"));
                collect_Button.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            case 2:
                if(search==null){
                    search= new SearchFragment();
                    transaction.add(R.id.id_content,search);
                }else {
                    transaction.show(search);
                }
                search_text.setTextColor(Color.parseColor("#1296db"));
                search_Button.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            case 3:
                if(me==null){
                    me= new MeFragment();
                    transaction.add(R.id.id_content,me);
                }else {
                    transaction.show(me);
                }
                me_text.setTextColor(Color.parseColor("#1296db"));
                me_Button.setImageResource(android.R.drawable.btn_star_big_on);
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if(home!=null){
            transaction.hide(home);
            shouye_text.setTextColor(Color.parseColor("#8a8a8a"));
        } if(collect!=null){
            transaction.hide(collect);
            collect_text.setTextColor(Color.parseColor("#8a8a8a"));
        } if(search!=null){
            transaction.hide(search);
            search_text.setTextColor(Color.parseColor("#8a8a8a"));
        } if(me!=null){
            transaction.hide(me);
            me_text.setTextColor(Color.parseColor("#8a8a8a"));
        }
    }

    private void initEvent() {
        id_shouye.setOnClickListener(this);
        id_collect.setOnClickListener(this);
        id_search.setOnClickListener(this);
        id_me.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        resetImges();
        switch (v.getId()){
            case R.id.id_shouye:
                setSelect(0);
                break;
            case R.id.id_collect:
                setSelect(1);
                break;
            case R.id.id_search:
                setSelect(2);
                break;
            case R.id.id_me:
                setSelect(3);
                break;
            default:
                break;
        }
    }

    protected void onResume() {
        super.onResume();
    }

    private void resetImges() {
        home_Button.setImageResource(android.R.drawable.btn_star_big_off);
        collect_Button.setImageResource(android.R.drawable.btn_star_big_off);
        search_Button.setImageResource(android.R.drawable.btn_star_big_off);
        me_Button.setImageResource(android.R.drawable.btn_star_big_off);
    }
}
